package com.yenlien.traceyun.control.service;

import com.yenlien.traceyun.control.dao.PositionDao;
import com.yenlien.traceyun.control.dao.ShelvesDao;
import com.yenlien.traceyun.control.dao.StoreHouseDao;
import com.yenlien.traceyun.control.entity.Shelves;
import com.yenlien.traceyun.control.service.rest.FileRestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jialong
 * @date 2021/05/18
 * @description: 仓库服务
 **/
@Service
public class ShelvesService {

    @Autowired
    private FileRestService fileRestService;

    @Autowired
    private StoreHouseDao storeHouseDao;

    @Autowired
    private PositionDao positionDao;

    @Autowired
    private ShelvesDao shelvesDao;


    @Transactional
    public Shelves save(Shelves shelves) {
        //


        return null;
    }


    public Page<Shelves> findList(Shelves shelves) {
        Pageable pageable = PageRequest.of(shelves.getPage(), shelves.getPageSize(), Sort.Direction.DESC, ("createTime"));
        return shelvesDao.findAll((Root<Shelves> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            //构造查询条件
            List<Predicate> list = new ArrayList<>();
            if (StringUtils.isNotBlank(shelves.getName())) {
                list.add(cb.equal(root.get("name"), "%" + shelves.getName() + "%"));
            }
            if (shelves.getPositionId() != null) {
                list.add(cb.equal(root.get("positionId"), shelves.getPositionId()));
            }
//            if(StringUtils.isNotBlank(shelves.getGoodsName())){
//                list.add(cb.equal(root.get("goodsName"), "%" + shelves.getGoodsName() + "%"));
//            }
//            if(StringUtils.isNotBlank(shelves.getGoodsNum())){
//                list.add(cb.equal(root.get("goodsNum"), shelves.getGoodsNum()));
//            }
//            if(StringUtils.isNotBlank(shelves.getGoodsValue())){
//                list.add(cb.equal(root.get("goodsValue"), shelves.getGoodsValue()));
//            }
//            if(StringUtils.isNotBlank(shelves.getGoodsLevel())){
//                list.add(cb.equal(root.get("goodsLevel"), shelves.getGoodsLevel()));
//            }
//            if(StringUtils.isNotBlank(shelves.getOwnerName())){
//                list.add(cb.equal(root.get("ownerName"), shelves.getOwnerName()));
//            }
//            if(StringUtils.isNotBlank(shelves.getType())){
//                list.add(cb.equal(root.get("type"), shelves.getType()));
//            }
//            if(StringUtils.isNotBlank(shelves.getBatch())){
//                list.add(cb.equal(root.get("batch"), shelves.getBatch()));
//            }
//            if(StringUtils.isNotBlank(shelves.getExpiration())){
//                list.add(cb.equal(root.get("expiration"), shelves.getExpiration()));
//            }
            Predicate[] predicate = new Predicate[list.size()];
            return cb.and(list.toArray(predicate));
        }, pageable);
    }


    public Shelves findById(Long id) {
        return shelvesDao.findById(id);
    }


}
